package com.earthchen.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.earthchen.demo.dao.SysRoleDao;
import com.earthchen.demo.domain.SysRole;
import com.earthchen.demo.domain.SysRoleResource;
import com.earthchen.demo.enums.ExceptionEnum;
import com.earthchen.demo.exception.AuthException;
import com.earthchen.demo.service.SysRoleResourceService;
import com.earthchen.demo.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author earthchen
 * @date 2018/9/4
 **/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole>
        implements SysRoleService {

    @Autowired
    private SysRoleResourceService sysRoleResourceService;


    @Override
    public boolean authorityRoleResource(Long roleId, Long resourceId) {
        SysRoleResource sysRoleResource = new SysRoleResource();
        sysRoleResource.setRoleId(roleId);
        sysRoleResource.setResourceId(resourceId);
        return sysRoleResourceService.insert(sysRoleResource);
    }

    @Override
    public void deleteAuthorityRoleResource(Long roleId, Long resourceId) {
        boolean success = sysRoleResourceService.delete(new EntityWrapper<SysRoleResource>()
                .where("role_id={0} and resource_id={1}", roleId, resourceId));
        if (!success) {
            throw new AuthException(ExceptionEnum.DELETE_ROLE_RESOURCE_ERROR);
        }

    }
}
